package com.xzj.admin.auth.handle;

import cn.dev33.satoken.stp.StpInterface;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.base.domain.SysRoleInfo;
import com.xzj.admin.base.domain.SysUserInfo;
import com.xzj.admin.boot.api.service.AdminBootService;
import com.xzj.common.core.constants.Constants;
import com.xzj.common.core.domain.vo.R;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: StpInterfaceImpl
 * @description: 实现获取当前账号权限角色集合
 * @author: XiaZunJun
 * @create: 2024-06-27 15:16
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private AdminBootService adminBootService;

    /**
     *当前全部是模拟数据，真实情况使用根据loginId动态查询对应角色和权限
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的权限列表
        List<String> strs = new ArrayList<>();
        R r = adminBootService.perCodeList(String.valueOf(loginId));
        // 检查获取用户信息是否成功
        if(Constants.SUCCESS.equals(r.getStatus()) && null != r.getResult()){
            strs = JSONArray.from(r.getResult()).toList(String.class);
        }
        return strs;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        R r = adminBootService.userInfoById(String.valueOf(loginId));
        // 返回此 loginId 拥有的角色列表
        List<String> strs = new ArrayList<>();
        // 检查获取用户信息是否成功
        if(Constants.SUCCESS.equals(r.getStatus()) && null != r.getResult()){
            // 获取用户信息数据
            // 返回登录成功后的用户对象
            SysUserInfo sysUserInfo = JSONObject.from(r.getResult()).to(SysUserInfo.class);
            for(SysRoleInfo role : sysUserInfo.getRoles()){
                strs.add(role.getRoleValue());
            }
        }
        return strs;
    }
}
